/*
 * Copyright (C) 2019 yixun All Rights Reserved.
 * 
 * ReportModelContentServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.ReportModelContentMapper;
import com.yixun.qfbao.mapper.ReportModelMapper;
import com.yixun.qfbao.model.ReportModel;
import com.yixun.qfbao.model.ReportModelContent;
import com.yixun.qfbao.model.dto.ReportModelContentDto;
import com.yixun.qfbao.model.dto.ReportModelDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.ReportModelContentService;
import com.yixun.qfbao.util.ChannelStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * [文件名称]<br>
 * ReportModelContentServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 *  service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2019-03-27 11:15:06 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("reportModelContentService")
public class ReportModelContentServiceImpl implements ReportModelContentService {
	
	@Autowired
	private ReportModelContentMapper mapper;

	@Autowired
	private ReportModelMapper reportModelMapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<ReportModelContent> getPageModel(ReportModelContentDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<ReportModelContent> rows = mapper.list(entity,begin,end);
	    Page<ReportModelContent> msr = new Page<ReportModelContent>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<ReportModelContent> selectListByDto(ReportModelContentDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public ReportModelContent selectByDto(ReportModelContentDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param id 搜索条件
	 * @return 分页记录
	 */
	public ReportModelContent selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public ReportModelContentDto create(ReportModelContentDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(ReportModelContentDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(ReportModelContentDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(ReportModelContentDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public ReportModelContent selectBy(ReportModelContent entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	/**
	 * 批量更新报备模板内容
	 * @param reportModelContent
	 * @param premisesIds
	 */
	@Override
	public void batchUpdateModelContent(ReportModelContentDto reportModelContent,List<Long> premisesIds){
		String content = reportModelContent.getContentParams();
		List<String> columns = ChannelStringUtils.getColumns(content);
		for(Long premisesId : premisesIds){
			StringBuilder sb = new StringBuilder();
			//获取楼盘所有的模板内容
			ReportModelDto dto = new ReportModelDto();
			dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			dto.setPremisesId(premisesId);
			List<ReportModel> list = reportModelMapper.selectListByDto(dto);
			Map<String,ReportModel> map = list.stream().collect(Collectors.toMap(ReportModel::getModelColumn, Function.identity()));
			//获取模板id
			for(String column : columns){
				if(map.containsKey(column)){
					sb.append(map.get(column).getId());
					sb.append(",");
				}
			}
			if(sb.length()>0){
				sb.deleteCharAt(sb.length()-1);
			}
			ReportModelContentDto reportModelContentDto = new ReportModelContentDto();
			reportModelContentDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			reportModelContentDto.setPremisesId(premisesId);
			List<ReportModelContent> reportModelContents = mapper.selectListByDto(reportModelContentDto);
			if(reportModelContents != null && reportModelContents.size() > 0){
				reportModelContentDto.setId(reportModelContents.get(0).getId());
			}
			reportModelContentDto.setContent(reportModelContent.getContent());
			reportModelContentDto.setContentParams(reportModelContent.getContentParams());
			reportModelContentDto.setModelIds(sb.toString());
			saveOrUpdate(reportModelContentDto);
		}
	}
	@Override
	public int editPremisesModel(ReportModelContentDto reportModelContentDto,Long premisesId){
		//主要处理标签数据，分析contentParams 里的标签，再将楼盘的标签遍历map一下
		List<String> params = ChannelStringUtils.getColumns(reportModelContentDto.getContentParams());
		ReportModelDto reportModelDto = new ReportModelDto();
		reportModelDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		reportModelDto.setPremisesId(premisesId);
		List<ReportModel> reportModels = reportModelMapper.selectListByDto(reportModelDto);
		Map<String,ReportModel> map = reportModels.stream().collect(Collectors.toMap(ReportModel::getModelColumn,Function.identity()));
		StringBuilder sb = new StringBuilder();
		for(String p : params){
			if(map.containsKey(p)){
				sb.append(map.get(p).getId());
				sb.append(",");
			}
		}
		if(sb.length() > 0){
			sb.deleteCharAt(sb.length()-1);
		}
		ReportModelContentDto dto = new ReportModelContentDto();
		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		dto.setPremisesId(premisesId);
		List<ReportModelContent> list = mapper.selectListByDto(dto);
		if(null != list && list.size() > 0){
            dto.setId(list.get(0).getId());
        }
        dto.setModelIds(sb.toString());
        dto.setContent(reportModelContentDto.getContent());
        dto.setContentParams(reportModelContentDto.getContentParams());

		return saveOrUpdate(dto);

	}
}
